﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
namespace LightSwitchApplication
{
    public partial class Prescription
    {
        /*
        partial void MedicalName_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            Boolean isInvalid = false;
            if(this.MedicalName != null){
                IDataServiceQueryable<Drug> drugs = this.DataWorkspace.ApplicationData.DrugsWithSameMedicalName(this.MedicalName);
                if(drugs.FirstOrDefault() == null){
                    results.AddPropertyError("Do not have any drug with the medical name:" + this.MedicalName);
                    isInvalid = true;
                }
                else
                    isInvalid = isUnFillable(results);
                if (isInvalid)
                    callPhysician(results);
            }
        }*/

        private Boolean isUnFillable(EntityValidationResultsBuilder results)
        {

            Boolean isInvalid = false;/*
            if (this.MedicalName != null && this.Dosage != 0)
            {
                foreach (Fillment f in this.DataWorkspace.ApplicationData.CurrentFillmentsForPatient(this.PrescriptionHeader.Patient.Id))
                {
                    String mName = f.Prescription.MedicalName;
                    int dosage = f.Prescription.Dosage;
                    foreach (Conflict c in this.DataWorkspace.ApplicationData.Conflicts)
                    {
                        if (c.MedicalName1 == this.MedicalName && (c.Dosage1 == null || c.Dosage1 == this.Dosage) &&
                            c.MedicalName2 == mName && (c.Dosage2 == null || c.Dosage2 == dosage) &&
                            c.MedicalName1 == this.MedicalName && (c.Dosage2 == null || c.Dosage2 == this.Dosage) &&
                            c.MedicalName1 == mName && (c.Dosage1 == null || c.Dosage1 == dosage))
                        {
                            results.AddPropertyError("Conflicted drugs: " + this.MedicalName + "/" + this.Dosage + "<->" + mName + "/" + dosage);
                            isInvalid = true;
                            break;
                        }
                    }
                }

                if (!isInvalid)
                {
                    DateTime now = DateTime.Today;
                    DateTime dob = PrescriptionHeader.Patient.DOB;
                    int age = now.Year - dob.Year;
                    if (dob > now.AddYears(-age)) age--;
                    if (DataWorkspace.ApplicationData.DrugsMeetingPrescritpion(MedicalName, Dosage, TakingDays * TakingTimesPerDay, age).FirstOrDefault() == null)
                    {
                        results.AddPropertyError("No suitable drug to fill this the prescription: " + MedicalName + "/" + Dosage);
                        isInvalid = true;
                    }
                }                
            }*/
            return (isInvalid);
        }


        partial void Tag_Compute(ref string result)
        {
            // Set result to the desired field value
            result = this.CreatedTime + "  (" + this.MedicalName + ")/" + this.Dosage + "mg" ;
        }

        partial void MedicalName_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            if(this.MedicalName != null)
                this.MedicalName = this.MedicalName.ToUpper().Trim();
        }

        partial void CommercialName_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            if (this.CommercialName != null)
                this.CommercialName = this.CommercialName.ToUpper().Trim();
        }
        
        /*
        private Boolean isFillable()
        {
            if (this.MedicalName == null || this.Patient == null || this.Dosage == 0 || this.TakingDays == 0 ||
                this.TakingTimesPerDay == 0)
                return false;


            Drug drugFoundByMN = this.DataWorkspace.ApplicationData.DrugsMeetingPrescritpion(this.MedicalName, this.Dosage,
                                    this.TakingTimesPerDay, this.TakingDays, this.Patient.DOB).FirstOrDefault();
            if (drugFoundByMN == null)
            {
                this.ErrMsg = "The prescription cannot be filled with drugs in stock.";
                return false;
            }
            else
            {
                this.ErrMsg = "";
                return true;
            }
        }*/




        /*
        partial void MedicalName_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");

            isFillable();
        }*/
    }
}
